package com.gaofans.nrpc.example.dict;

import com.gaofans.nrpc.common.processor.RpcContext;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class DictServiceImpl implements DictService{
    private static Map<String,Dict> data = new ConcurrentHashMap<>();

    @Override
    public Mono<Void> addDict(String dictCode, String dictName, String dictTips, String dictValues) throws IllegalStateException {
        System.out.println(Thread.currentThread().getName()+ " " +RpcContext.current().get("token"));
        Dict dict = new Dict();
        dict.setCode(dictCode);
        dict.setName(dictName);
        dict.setNote(dictTips);
        dict.setNum(Integer.parseInt(dictValues));
        data.put(dictCode,dict);
        System.out.println("add dict....");
        return Mono.empty();
    }

    @Override
    public Mono<Void> editDict(String dictId, String dictCode, String dictName, String dictTips, String dicts) throws IllegalStateException {
        return null;
    }

    @Override
    public Mono<Void> delteDict(String dictId) {
        return null;
    }

    @Override
    public Mono<List<Dict>> selectByCode(String id) {
        return Mono.just(Collections.singletonList(data.get(id)));
    }

    @Override
    public Flux<Dict> selectByParentCode(String code) {
        Set<Map.Entry<String, Dict>> entries = data.entrySet();
        List<Dict> dicts = new ArrayList<>();
        for (Map.Entry<String, Dict> entry : entries) {
            dicts.add(entry.getValue());
        }
        Dict[] res = new Dict[dicts.size()];
        dicts.toArray(res);
        return Flux.just(res);
    }

    @Override
    public Flux<Map<String, Object>> list(String conditiion) {
        return null;
    }

    @Override
    public Mono<List<Dict>> selecyByPid(String dictId) {
        return null;
    }

    @Override
    public Mono<WebPage<Dict>> findPage(WebPage<Dict> webPage, String condition) {
        return null;
    }
}
